package eirkdbd.lab1.log.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import eirkdbd.lab1.data.Team;
import eirkdbd.lab1.data.TeamDataLoader;
import eirkdbd.lab1.fuzzyqueries.BipolarQuery;
import eirkdbd.lab1.fuzzyqueries.circumstances.Circumstance;
import eirkdbd.lab1.membership.MembershipFunction;
import eirkdbd.lab1.membership.functions.ComposedMembershipFunction;
import eirkdbd.lab1.membership.functions.CustomMembershipFunction;
import eirkdbd.lab1.membership.functions.helpers.norms.Norm;
import eirkdbd.lab1.membership.functions.helpers.norms.implications.Implication;
import eirkdbd.lab1.membership.functions.helpers.norms.tnorms.Tnorm;
import eirkdbd.lab1.obj.Function;
import eirkdbd.lab1.obj.TableTeam;

public class TeamsModel implements TableModel<TableTeam> {

	private static TeamsModel model = new TeamsModel();
	
	private List<TableTeam> tableTeams;
	
	private Implication implication;
	
	private Tnorm tnorm;
	
	private Circumstance W;
	
	private TeamsModel() {
		tableTeams = new ArrayList<TableTeam>();
	}
	
	public static TeamsModel getInstance() {
		return model;
	}
	
	public Iterator<TableTeam> getModelObjects() {
		clear();
		
		//lista warunkow koniecznych
		List<Norm> norms = new ArrayList<Norm>();
		List<MembershipFunction> mfs = new ArrayList<MembershipFunction>();
		Iterator<Function> it = MainConditionsModel.getInstance().getModelObjects();
		while(it.hasNext()) {
			Function f = it.next();
			mfs.add(new CustomMembershipFunction(f.getMax(), f.getMin(),
					f.getStatisticName()));
			if(f.getJoin() != null)
				norms.add(f.getJoin().getNorm());
		}
		MembershipFunction C = null;
		if(mfs.size() > 1)
			C = new ComposedMembershipFunction(mfs, norms);
		else
			C = mfs.get(0);
		
		//lista warunkow wystarczajacych
		List<Norm> norms2 = new ArrayList<Norm>();
		List<MembershipFunction> mfs2 = new ArrayList<MembershipFunction>();
		Iterator<Function> it2 = SubConditionsModel.getInstance().getModelObjects();
		while(it2.hasNext()) {
			Function f = it2.next();
			mfs2.add(new CustomMembershipFunction(f.getMax(), f.getMin(),
					f.getStatisticName()));
			if(f.getJoin() != null)
				norms.add(f.getJoin().getNorm());
		}
		MembershipFunction P = null;
		if(mfs2.size() > 1)
			P = new ComposedMembershipFunction(mfs2, norms2);
		else
			P = mfs2.get(0);
		
		TeamDataLoader tdl;
		List<Team>  teams;
		try {
			tdl = new TeamDataLoader();
			teams = tdl.getTeamData();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return tableTeams.iterator();
		}
		try {
			BipolarQuery bq = new BipolarQuery(C, P, W, teams, tnorm, implication);
			for (Entry<Team, Double> entry : bq.execute().entrySet()) {
				tableTeams.add(new TableTeam(entry.getKey(), entry.getValue()));
			}
		}
		catch (IllegalArgumentException iae){
			System.out.println(iae.getMessage());
		}
		
		return tableTeams.iterator();
	}
	
	public Implication getImplication() {
		return implication;
	}

	public void setImplication(Implication implication) {
		this.implication = implication;
	}

	public Tnorm getTnorm() {
		return tnorm;
	}

	public void setTnorm(Tnorm tnorm) {
		this.tnorm = tnorm;
	}

	public Circumstance getW() {
		return W;
	}

	public void setW(Circumstance w) {
		W = w;
	}

	public void clear() {
		tableTeams.clear();
	}
	
	public void add(TableTeam obj) {
		//nothing to do
	}
}